﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;

namespace Square.Activity.ActivityTestWP
{
    [ToolboxItemAttribute(false)]
    public class ActivityTestWP : Microsoft.SharePoint.WebPartPages.WebPart
    {
        Button BtnSave;
        Button BtnSet;
        TextBox txtMemo;
        SquareActivityHelper sHelper;
        RadioButtonList rblType;
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            rblType = new RadioButtonList();
            rblType.RepeatDirection = RepeatDirection.Vertical;
            foreach (SquareActivities actType in Enum.GetValues(typeof(SquareActivities)))
            {
                if (!actType.ToString().Contains("All"))
                {
                    rblType.Items.Add(actType.ToString());
                }
            }
            rblType.SelectedIndexChanged += new EventHandler(rblType_SelectedIndexChanged);
            rblType.AutoPostBack = true;
            Controls.Add(rblType);

            BtnSet = new Button();
            BtnSet.Text = "초기셋팅";
            BtnSet.Click += new EventHandler(BtnSet_Click);
            Controls.Add(BtnSet);

            BtnSave = new Button();
            BtnSave.Text = "저장";
            BtnSave.Click += new EventHandler(BtnSave_Click);
            Controls.Add(BtnSave);

            txtMemo = new TextBox();
            txtMemo.TextMode = TextBoxMode.MultiLine;
            txtMemo.Rows = 6;
            txtMemo.Width = (Unit)500;
            txtMemo.Text = "여기에 입력";
            Controls.Add(txtMemo);

        }

        void rblType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (rblType.SelectedValue)
            {
                case "SquareDocFolderCreate": txtMemo.Text = "<a href='http://www.naver.com'>xxx</a>폴더를 생성했습니다."; break;
                case "SquareDocFolderPermission": txtMemo.Text = "<a href='http://www.naver.com'>xxx</a>폴더의 권한을 변경했습니다."; break;
                case "SquareDocUpload": txtMemo.Text = "문서를 등록했습니다."; break;
                case "SquareMemoCommentWrite": txtMemo.Text = "<a href='http://nsdesktop/my/person.aspx?accountname=NSDEV%5CAdministrator'>xxx</a>님의 글에 댓글을 등록했습니다."; break;
                case "SquareMemoGroupWrite": txtMemo.Text = "<a href='http://www.naver.com'>xxx</a>그룹에 글을 등록했습니다."; break;
                case "SquareMemoWrite": txtMemo.Text = "<a href='http://nsdesktop/my/person.aspx?accountname=NSDEV%5CAdministrator'>xxx</a>님에게 글을 등록했습니다."; break;
                case "SquareMemoShareWrite": txtMemo.Text = "공유글을 등록했습니다."; break;
                case "SquarePeopleAccept": txtMemo.Text = "<a href='http://nsdesktop/my/person.aspx?accountname=NSDEV%5CAdministrator'>xxx</a>님의 동료요청을 수락했습니다."; break;
                case "SquarePeopleRequest": txtMemo.Text = "<a href='http://nsdesktop/my/person.aspx?accountname=NSDEV%5CAdministrator'>xxx</a>님에게 동료신청을 했습니다."; break;
                case "SquarePeopleGroupAddPeople": txtMemo.Text = "<a href='http://www.naver.com'>xxx</a>그룹에 동료를 추가했습니다."; break;
                case "SquarePeopleGroupCreate": txtMemo.Text = "<a href='http://www.naver.com'>xxx</a>그룹을 생성했습니다."; break;
            }
            
        }

        void BtnSet_Click(object sender, EventArgs e)
        {
            sHelper = new SquareActivityHelper();
            sHelper.SetupActivities(this.Page);
        }


        void BtnSave_Click(object sender, EventArgs e)
        {
            bool bSave = false;
            SquareActivities aType = new SquareActivities();
            switch (rblType.SelectedValue)
            {
                case "SquareDocFolderCreate": aType = SquareActivities.SquareDocFolderCreate; break;
                case "SquareDocFolderPermission": aType = SquareActivities.SquareDocFolderPermission; break;
                case "SquareDocUpload":aType = SquareActivities.SquareDocUpload;break;
                case "SquareMemoCommentWrite": aType = SquareActivities.SquareMemoCommentWrite; break;
                case "SquareMemoGroupWrite": aType = SquareActivities.SquareMemoGroupWrite; break;
                case "SquareMemoWrite":aType = SquareActivities.SquareMemoWrite;break;
                case "SquareMemoShareWrite": aType = SquareActivities.SquareMemoShareWrite; break;
                case "SquarePeopleAccept":aType = SquareActivities.SquarePeopleAccept;break;
                case "SquarePeopleRequest":aType = SquareActivities.SquarePeopleRequest;break;
                case "SquarePeopleGroupAddPeople": aType = SquareActivities.SquarePeopleGroupAddPeople; break;
                case "SquarePeopleGroupCreate": aType = SquareActivities.SquarePeopleGroupCreate; break;
            }
            
            sHelper = new SquareActivityHelper();
            bSave = sHelper.SaveActivity(this.Page, aType, txtMemo.Text);
        }


        protected override void Render(HtmlTextWriter writer)
        {
            BtnSet.RenderControl(writer);
            writer.WriteLine("</br>");
            rblType.RenderControl(writer);
            writer.WriteLine("</br>");
            txtMemo.RenderControl(writer);
            writer.WriteLine("</br>");
            BtnSave.RenderControl(writer);

            #region 이전테스트코드
            //DataSet ds = null;
            //string sType = string.Empty;
            //if (Page.Request.Params["sType"] != null)
            //{
            //    sType = Page.Request.Params["sType"] as string;
            //}

            //SquareActivities aType = new SquareActivities();
            //switch (sType)
            //{
            //    case "Memo": aType = SquareActivities.MemoAll; break;
            //    case "Doc": aType = SquareActivities.DocAll; break;
            //    case "People": aType = SquareActivities.PeopleAll; break;
            //}

            //if (string.IsNullOrEmpty(sType))
            //{
            //    aType = SquareActivities.MemoAll;
            //}

            //sHelper = new SquareActivityHelper();
            //ds = sHelper.GetActivity(this.Page, aType);

            //BtnSet.RenderControl(writer);
            //writer.WriteLine("</br>");
            //rblType.RenderControl(writer);
            //writer.WriteLine("</br>");
            //txtMemo.RenderControl(writer);
            //writer.WriteLine("</br>");
            //BtnSave.RenderControl(writer);
            //writer.WriteLine("</br></br>");
            //writer.WriteLine("<table border=\"1\" cellpadding=\"5\" cellspacing=\"2\">");
            //writer.WriteLine("    <tr>");
            //writer.WriteLine("        <td>사진</td><td>Publisher</td><td>사진</td><td>Owner</td><td>내용</td><td>날짜</td><td>시간</td><td>타입</td><td>기타</td>");
            //writer.WriteLine("    </tr>");
            //foreach (DataRow dr in ds.Tables[0].Rows)
            //{
            //    writer.WriteLine("    <tr>");
            //    writer.WriteLine("        <td><img src=\"" + dr["PublisherPic"] + "\" alt=\"" + dr["Publisher"] + "\"></img></td>");
            //    writer.WriteLine("        <td><a href=\"" + dr["PublisherLink"] + "\">" + dr["Publisher"] + "</a></td>");
            //    writer.WriteLine("        <td><img src=\"" + dr["OwnerPic"] + "\" alt=\"" + dr["Owner"] + "\"></img></td>");
            //    writer.WriteLine("        <td><a href=\"" + dr["OwnerLink"] + "\">" + dr["Owner"] + "</a></td>");
            //    writer.WriteLine("        <td>" + dr["Value"] + "</td>");
            //    writer.WriteLine("        <td>" + dr["ActivityDate"] + "</td>");
            //    writer.WriteLine("        <td>" + dr["ActivityTime"] + "</td>");
            //    writer.WriteLine("        <td>" + dr["ActivityType"] + "</td>");
            //    writer.WriteLine("        <td>" + dr["Reserved"] + "</td>");
            //    writer.WriteLine("    </tr>");
            //}

            //writer.WriteLine("</table>");
            #endregion
        }
    }
}
